package com.L.yygh.hosp.controller.api;

import com.L.yygh.common.exception.YyghException;
import com.L.yygh.common.result.Result;
import com.L.yygh.common.result.ResultCodeEnum;
import com.L.yygh.helper.HttpRequestHelper;
import com.L.yygh.hosp.service.DepartmentService;
import com.L.yygh.hosp.service.HospitalSetService;
import com.L.yygh.model.hosp.Department;
import com.L.yygh.util.MD5;
import com.L.yygh.vo.hosp.DepartmentQueryVo;
import com.alibaba.excel.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api(tags = "科室管理api接口")
@RestController
@RequestMapping("/api/hosp")
public class DepartmentApiController {

    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private HospitalSetService hospitalSetService;

    //上传科室接口
    @ApiOperation("上传科室接口")
    @PostMapping("/saveDepartment")
    public Result saveHosp(HttpServletRequest request){
        //获取传递过来医院的信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //1.获取医院系统中传递过来的签名,这个签名进行了Md5的加密
        String sign = (String)paramMap.get("sign");
        //2.根据传递过来的医院编号，查询数据库，看签名是否一样
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if(!sign.equals(signKeyMd5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }else{
            //调用service方法
            departmentService.save(paramMap);
            return Result.ok();


        }
    }

    //查询科室信息
    @ApiOperation("查询科室接口")
    @PostMapping("/department/list")
    public Result findDepartment(HttpServletRequest request){
        //获取传递过来医院的信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //1.获取医院系统中传递过来的签名,这个签名进行了Md5的加密
        String sign = (String)paramMap.get("sign");
        //2.根据传递过来的医院编号，查询数据库，看签名是否一样
        String hoscode = (String) paramMap.get("hoscode");
        //当前页和每页记录数
        int currentPage = StringUtils.isEmpty((String) paramMap.get("page"))?1:Integer.parseInt((String) paramMap.get("page"));
        int limitPage = StringUtils.isEmpty((String) paramMap.get("limit"))?1:Integer.parseInt((String) paramMap.get("limit"));

        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if(!sign.equals(signKeyMd5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }else{
            DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
            departmentQueryVo.setHoscode(hoscode);
            //调用service中的方法来操作
            Page<Department> pageModel = departmentService.findPageDeparement(currentPage,limitPage,departmentQueryVo);
            return Result.ok(pageModel);

        }
    }

    //删除科室接口
    @ApiOperation("删除科室接口")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //获取传递过来医院的信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //1.获取医院系统中传递过来的签名,这个签名进行了Md5的加密
        String sign = (String)paramMap.get("sign");
        //2.根据传递过来的医院编号，查询数据库，看签名是否一样
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库查询签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if(!sign.equals(signKeyMd5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }else{

            departmentService.remove(hoscode,depcode);
            return Result.ok();

        }
    }
}
